package com.lancoo.examview.exam;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.entity.ExamRequireBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.paperUtils.PaperDataUtils;
import com.lancoo.answer.view.result.ExamResultFragment;
import com.lancoo.answer.widget.combinationView.CompositionTestAnswerSheetView;
import com.lancoo.examview.R;
import com.lancoo.examview.utils.ConvertStremUtils;
import com.rxjava.rxlife.RxLife;

import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pengmi on 2022/2/22 16:12
 * Description:结果
 */
public class ResultAnswerSheetActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_sheet_result);
        initData();
    }




    public static void launch(Context mContext, String dataAsset, int trainSence){
        Intent mIntent=new Intent(mContext, ResultAnswerSheetActivity.class);
        mIntent.putExtra("dataAsset",dataAsset);
        mIntent.putExtra("trainSence",trainSence);
        mContext.startActivity(mIntent);
    }


    private void initData(){

        Observable.create((ObservableOnSubscribe<List<Type>>) emitter -> {
                    List<Type> list;
                    String originalFundData = getOriginalFundData();
                    Gson gson=new Gson();
                    list = gson.fromJson(originalFundData, new TypeToken<List<Type>>(){}.getType());
                    emitter.onNext(list);
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(RxLife.as(this))
                .subscribe(this::initView, throwable -> {});
    }

    private void initView(final List<Type> typeList){
        int trainSence = getIntent().getIntExtra("trainSence", 0);

        Observable.create((ObservableOnSubscribe<List<AnswerSheetBean>>) emitter -> {
                    TaskControlBean controlBean=new TaskControlBean();
                    ExamRequireBean examRequestBean=new ExamRequireBean();
                    examRequestBean.setExamName("666考试"+trainSence);
                    examRequestBean.setExamTimeLength(120);
                    examRequestBean.setTotalQuesCount(50);
                    controlBean.setTrainSence(1);
                    controlBean.setPaperType(1);
                    controlBean.setExamRequireBean(examRequestBean);
                    controlBean.setTaskState(TaskState.LOOKING);
                    ConstantBean.Companion.setTaskControlBean(controlBean);
                    ExamViewBean examViewBean=new ExamViewBean();
                    examViewBean.setTypeList(typeList);
                    List<AnswerSheetBean> answerSheetBeanList=
                            PaperDataUtils.loadAnswerSheetData(typeList);
                    emitter.onNext(answerSheetBeanList);

                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(RxLife.as(this))
                .subscribe(answerSheetBeanList -> {
                    ExamResultFragment fragment = new ExamResultFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.framelayout, fragment, "myTag")
                            .commitAllowingStateLoss();

                }, throwable -> {});

    }


    private String getOriginalFundData(){
        InputStream inputStream;
        try{
            String dataAsset=getIntent().getStringExtra("dataAsset");
            inputStream=getAssets().open(dataAsset);
            String json=convertStremToString(inputStream);
            inputStream.close();
            return json;
        }catch (Exception e){
            e.printStackTrace();
            Log.e("eee","Exception"+e.toString());
        }
        return null;
    }

    private String convertStremToString(InputStream is){
        String s=null;
        try{
            Scanner scanner=new Scanner(is,"UTF-8").useDelimiter("\\A");
            if(scanner.hasNext()){
                s=scanner.next();
            }
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }


}
